package com.lysj.order.customer.controller;

import com.lysj.admin.common.merchant.domain.ComDeskVo;
import com.lysj.admin.utils.constant.CommonConstant;
import com.lysj.admin.utils.spring.foundation.base.BaseContent;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.admin.utils.web.Resp;
import com.lysj.order.customer.vo.CusOrderVo;
import com.lysj.order.order.domain.Order;
import com.lysj.order.order.feign.DeskFeign;
import com.lysj.order.order.service.OrderItemService;
import com.lysj.order.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @ Author ：drj.
 * @ Date  ：Created in 11:31 2019/3/28
 * @ Description:
 **/
@Slf4j
@RestController
@RequestMapping("/order/management/customer")
public class CustomerController extends BaseContent {

    @Resource
    private OrderService orderService;

    @Resource
    private OrderItemService orderItemService;


    /*
     * @author drj
     * @date 2019-04-02 16:27
     * @Description :获取我的订单
     */
    @GetMapping("/list")
    public Resp list(String customerId) {

        //查询用户订单
        List<Order> orderList = orderService.getRepository().findByCustomerIdAndDelFlagOrderByCreateTimeDesc(customerId, CommonConstant.NORMAL_FLAG);
        List<CusOrderVo> cusOrderVos = new LinkedList<>();

        //遍历订单
        for (Order order : orderList) {
            CusOrderVo cusOrderVo = new CusOrderVo();
            cusOrderVo.setOrderId(order.getId());
            cusOrderVo.setCreateTime(order.getCreateTime());
            cusOrderVo.setStatus(order.getStatus());
            //点菜数量
            cusOrderVo.setFoodAmount(orderItemService.getRepository().countFood(order.getId()));
            //金额
            cusOrderVo.setMoney(null == order.getActPayMoney() ? orderItemService.getRepository().summaryMoney(order.getId()) : order.getActPayMoney()); //实付金额
            cusOrderVos.add(cusOrderVo);
        }
        return new Resp().success(cusOrderVos);
    }


    /*
     * @author drj
     * @date 2019-04-02 16:27
     * @Description :查看订单详情
     */
    @GetMapping("/order_detail")
    public Resp orderDetail(String orderId) {
        return new Resp().success(orderService.orderDetail(orderId));
    }

    /*
     * @author drj
     * @date 2019-04-01 16:58
     * @Description :点击订单支付
     */
    @PostMapping("/unifiedorder")
    public Resp unifiedorder(String orderId, Integer payWay) {
        return new Resp().success(orderService.orderPay(orderId, payWay));
    }


    /*
     * @author drj
     * @date 2019-04-09 11:47
     * @Description :发起退款申请
     */
    @PostMapping("/refund_apply")
    public Resp refundApply(String orderId) {
        Order order = orderService.findOne(orderId);
        order.setStatus(Order.Status.REFUNDAPPLY.getCode());
        orderService.save(order);
        return new Resp().success("操作成功");
    }


}
